import UICore from "../../../../Core/Res/UICore";
import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import AudioManager from "../../../../Core/Audio/AudioManager";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import { GameModels } from "../../socket/GameModels";
import { LocalUtil } from "../../../../Core/Utils/LocalUtil";

export class SettingWindow extends AbstractUIWindow {
    private bgmusic: fgui.GButton;
    private effectIsbtn: fgui.GButton;
    private vibrate: fgui.GButton;
    private headurl: fgui.GLoader;
    private nickname: fgui.GComponent;
    private slider: fgui.GSlider;
    protected getResList(): Array<string> {
        return [`Main`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Main", "SettingWindow").asCom;
    }
    protected onShow(): void {
        this.bgmusic.onClick(this.clickbgmusic, this);
        this.effectIsbtn.onClick(this.clickeffectIsbtn, this);
        this.vibrate.onClick(this.clickevibrate, this);
        this.bgmusic.selected = AudioManager.ins.isPlayBg;
        this.effectIsbtn.selected = AudioManager.ins.isPlayShot;
        this.vibrate.selected = SdkTool.ins.isShock == 1;
        this.headurl.url = GameModels.info.playerInfo.headUrl;
        this.nickname.getChild(`nametxt`).text = GameModels.info.playerInfo.userName;
        this.slider.max = 100;
        this.slider.on(fgui.Event.STATUS_CHANGED, this.sliderChanged, this);
        const _d = LocalUtil.ins.getCookie(`game_volume`);
        if (_d) {
            this.slider.value = Number(_d);
        } else {
            this.slider.value = 100;
        }
        this.headurl.onClick(this.clickheadurl, this);
    }
    private clickheadurl(): void {
        GameModels.lobby.send_ReqUploadWXInfo();
    }
    private sliderChanged(): void {
        LocalUtil.ins.setCookie(`game_volume`, this.slider.value);
        AudioManager.ins.setVolume(this.slider.value / 100);
    }
    private clickbgmusic(): void {
        AudioManager.ins.isplayMusic(this.bgmusic.selected);
    }
    private clickeffectIsbtn(): void {
        AudioManager.ins.isplayShot(this.effectIsbtn.selected);
    }
    private clickevibrate(): void {
        SdkTool.ins.isShock = this.vibrate.selected ? 1 : 2;
    }
}